#!/usr/bin/env python
# -*- coding:utf-8 -*-

import json
import requests


def jsonall():
    print json.__all__
    data = [{"name": "qiwsir", "lang": ("python", "english"), "age": 40}]
    data_json = json.dumps(data)
    print data_json
    data_j = json.dumps(data, sort_keys=True, indent=2)
    print data_j


def request02():
    r = requests.get("http://www.itdiffer.com")
    payload = {"key1": "value1", "key2": "value2"}
    r1 = requests.post("http://httpbin.org/post", data=payload)


class Student(object):  # Student对象不是一个可序列化为JSON的对象。
    def __init__(self, name, age, score):
        self.name = name
        self.age = age
        self.score = score

    def student2dict(std):  # 转换函数
        return {
            'name': std.name,
            'age': std.age,
            'score': std.score
        }

    def dict2student(d):
        return Student(d['name'], d['age'], d['score'])

    # def toObj(self, str):
    #     print(json.loads(json_str, object_hook=dict2student(str)))


if __name__ == '__main__':
    jsonall()
    s = Student('Bob', 20, 88)
    # print(json.dumps(s))
    # print(json.dumps(s, default=s.student2dict))
    print(json.dumps(s, default=lambda obj: obj.__dict__))  # 通用方法object转json
    json_str = '{"age": 20, "score": 88, "name": "Bob"}'
    Student.toObj(json_str)
